import pandas as pd
import numpy as np



pdCardZd = pd.read_excel("D:/yrProj/202401_评分卡/在贷.xlsx")
print(pdCardZd)
print(pdCardZd.columns.tolist())
# ['vars', 'varScore', 'bins']


pdCardJq = pd.read_excel("D:/yrProj/202401_评分卡/结清.xlsx")
print(pdCardJq)
print(pdCardJq.columns.tolist())
# ['vars', 'varScore', 'bins']


def parse_bins(bins):
  binPair = (-1,-1)
  if bins[0]=='[': # and bins[-1]==")":
    bins = bins[1:-1]
    vBins = bins.split(",")
    b1 = float(vBins[0])
    b2 = float(vBins[1])
    binPair = (b1,b2)
#  else:
#    if bins=="-1":
#      binPair = -1
  return binPair


def load_crads(pdCard):
  dCardsVals = {}
  for idx,row in pdCard.iterrows():
    var = str(row['vars'])
    varScore = float(row['varScore'])
    bins = str(row['bins'])
    print(var + " - " + str(varScore) + " - " + bins)

    if var == "intercept":
      dCardsVals[var] = varScore
    else:
      var = var.replace("_", "")
      if var not in dCardsVals:
        dCardsVals[var] = {}
      binPair = parse_bins(bins)
      print(binPair)
      dCardsVals[var][binPair] = varScore
      
  return dCardsVals

dCardsZd = load_crads(pdCardZd)
print(dCardsZd)

dCardsJq = load_crads(pdCardJq)
print(dCardsJq)


def calc_cards_val(dCard, dSc): #s1,s2,s3,s4,s5,s6,s7,s8,s9):
  val = dCard['intercept']
  for sC in dSc:
    if sC in dCard:
      dBinsVal = dCard[sC]
      cSc = dSc[sC]
      if cSc==-1:
        val = val + dBinsVal[(-1,-1)]
      else:
        for cBin in dBinsVal:
          if cSc>=cBin[0] and cSc<cBin[1]:
            val = val + dBinsVal[cBin]
  return val

pdPtn = pd.read_csv("./YRZX_10W_9个子分_20250207-pac_submodel_v1-result.csv")
#pdPtn = pd.read_csv("./银融致信_0206-md5-9个子分-样本量136569.csv")
print(pdPtn)
print(pdPtn.columns.tolist())
# [4029 rows x 18 columns]
# ['手机号', '身份证号', 'score1', 'score2', 'score3', 'score4', 'score5', 'score6', 'score7', 'score8', 'score9', '融合分', '融合分V34结果', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15', 'Unnamed: 16', 'Unnamed: 17']

fOut = open("./9子分10w_v1v2", "w")
#fOut = open("./9子分136569_v1v2", "w")
for idx,row in pdPtn.iterrows():
  s1 = float(row['score_1'])
  s2 = float(row['score_2'])
  s3 = float(row['score_3'])
  s4 = float(row['score_4'])
  s5 = float(row['score_5'])
  s6 = float(row['score_6'])
  s7 = float(row['score_7'])
  s8 = float(row['score_8'])
  s9 = float(row['score_9'])
  
  dSc = {"score1":s1, "score2":s2, "score3":s3, "score4":s4, "score5":s5, "score6":s6, "score7":s7, "score8":s8, "score9":s9}
  
  zdVal = calc_cards_val(dCardsZd, dSc) # s1,s2,s3,s4,s5,s6,s7,s8,s9)
  jqVal = calc_cards_val(dCardsJq, dSc) # s1,s2,s3,s4,s5,s6,s7,s8,s9)
  fOut.write(str(zdVal) + "\t" + str(jqVal) + "\n")


fOut.close()



